﻿using System;
using HalconDotNet;
using PLCDEMO;
using System.Diagnostics;

namespace ImagePro
{
    
    public partial class HDevelopExport
    {
        public HTuple hv_ExpDefaultWinHandle;
        public HObject dstImage;
        public HTuple area;
        private HTuple c, l;
        public  static int ar;
        DateTime beforDT, afterDT;
        public static TimeSpan ts;
        public static bool finish_status { get; set; }
        //public static event Form2.Mydelegate my;
        public HDevelopExport()
        {
            //my += Form2.DataChangeEventArgs;
            finish_status = false;
        }
        // Main procedure 
        private HObject action(HObject srcImage)
        {
            beforDT = DateTime.Now;
            // Local iconic variables 
            //Tuple<int, int> t = my();
            HObject ho_Image, ho_GrayImage, ho_Regions;
           
            HObject ho_ConnectedRegions, ho_SelectedRegions, ho_ImageReduced;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            //Image Acquisition 01: Code generated by Image Acquisition 01
            ho_Image.Dispose();
            HOperatorSet.CopyImage(srcImage, out ho_Image);
            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
            ho_Regions.Dispose();
            Console.WriteLine(Form2.low + " " + Form2.high);
            HOperatorSet.Threshold(ho_GrayImage, out ho_Regions, Form2.low, Form2.high);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);

            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                "and", 39118.1, 403272);
            HOperatorSet.AreaCenter(ho_SelectedRegions,out area,out c,out l);
            ho_ImageReduced.Dispose();
           
            HOperatorSet.ReduceDomain(ho_GrayImage, ho_SelectedRegions, out ho_ImageReduced);

            ho_Image.Dispose();
            ho_GrayImage.Dispose();
            ho_Regions.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            this.dstImage = ho_ImageReduced;

            afterDT = System.DateTime.Now;
             
            finish_status = true;
            try
            {
                ar = (int)area.D;
                ts = afterDT.Subtract(beforDT);
            }
            catch(Exception E)
            {
                Debug.Write(E.ToString());
            }
            
            //ho_ImageReduced.Dispose();
            return ho_ImageReduced;
        }

        public void InitHalcon()
        {
            // Default settings used in HDevelop 
            
            HOperatorSet.SetSystem("width", 512);
            HOperatorSet.SetSystem("height", 512);
        }

        public HObject RunHalcon(  HObject image ,HTuple WindowID)
        {
            hv_ExpDefaultWinHandle = WindowID;
            return action(image);
        }

        
    }

}